package com.java.controller;

import com.java.entity.Blogger;
import com.java.service.BloggerService;
import com.java.utils.CryptographyUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author wmx
 * @create 2020-03-07 18:04
 */
@Controller
@RequestMapping("/blogger")
public class BloggerContorller {
    @Resource
    private BloggerService bloggerService;


    @RequestMapping("login")
    public String login(Blogger blogger, HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();//获取当前登录用户
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(blogger.getUserName(),CryptographyUtil.md5(blogger.getPassword(),"java"));
        try{
            subject.login(usernamePasswordToken);//调用自封装用户名 的realm方法，验证用户信息
            return "redirect:/admin/main.jsp";
        }catch (Exception e){
            e.printStackTrace();
            request.setAttribute("blogger",blogger);
            request.setAttribute("errorInfo","用户名或密码错误！！！");
            return "login";
        }
    }

    /**
     * 关于博主
     */
    @RequestMapping("/about")
    public ModelAndView getAbountMe(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("mainPage","foreground/blogger/blogger.jsp");
        modelAndView.addObject("pageTitle","关于博主");
        modelAndView.setViewName("mainTemp");
        return modelAndView;
    }


}
